home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / SCREEN / CURSES01 / minix / c / newwin < prev    next >
Text File  |  1991-05-05  |  6KB  |  187 lines

  1. /****************************************************************/
  2. /* Newwin(), subwin() routines of the PCcurses package        */
  3. /*                                */
  4. /****************************************************************/
  5. /* This version of curses is based on ncurses, a curses version    */
  6. /* originally written by Pavel Curtis at Cornell University.    */
  7. /* I have made substantial changes to make it run on IBM PC's,    */
  8. /* and therefore consider myself free to make it public domain.    */
  9. /*        Bjorn Larsson (...mcvax!enea!infovax!bl)    */
  10. /****************************************************************/
  11. /* 1.0:    Release:                    870515    */
  12. /****************************************************************/
  13. /* Modified to run under the MINIX operating system by Don Cope */
  14. /* These changes are also released into the public domain.      */
  15. /*                             900906  */
  16. /****************************************************************/
  17.  
  18. #include <stdio.h>
  19. #include <curses.h>
  20. #include "curspriv.h"
  21.  
  22. /****************************************************************/
  23. /* Makenew() allocates all data for a new window except the    */
  24. /* actual lines themselves.                    */
  25. /****************************************************************/
  26.  
  27. static WINDOW *makenew(num_lines, num_columns, begy, begx)
  28.   int    num_lines, num_columns, begy, begx;
  29.   {
  30.   int         i;
  31.   WINDOW    *win;
  32.  
  33.   /* allocate the window structure itself */
  34.  
  35.   if ((win = (WINDOW *) malloc(sizeof(WINDOW))) == NULL)
  36.     return ((WINDOW *) ERR);
  37.  
  38.   /* allocate the line pointer array */
  39.  
  40.   if ((win->_line = (int **) calloc(num_lines, sizeof (int *))) == NULL)
  41.     {
  42.     free(win);
  43.     return((WINDOW *) ERR);
  44.     }
  45.  
  46.   /* allocate the minchng and maxchng arrays */
  47.  
  48.   if ((win->_minchng = (int *) calloc(num_lines, sizeof(int))) == NULL)
  49.     {
  50.     free(win);
  51.     free(win->_line);
  52.     return((WINDOW *) ERR);
  53.     }
  54.   if ((win->_maxchng = (int *) calloc(num_lines, sizeof(int))) == NULL)
  55.     {
  56.     free(win);
  57.     free(win->_line);
  58.     free(win->_minchng);
  59.     return((WINDOW *) ERR);
  60.     }
  61.  
  62.   /* initialize window variables */
  63.  
  64.   win->_curx      = 0;
  65.   win->_cury      = 0;
  66.   win->_maxy      = num_lines - 1;
  67.   win->_maxx      = num_columns - 1;
  68.   win->_begy      = begy;
  69.   win->_begx      = begx;
  70.   win->_flags     = 0;
  71.   win->_attrs     = ATR_NRM;
  72.   win->_colors      = 0x700;        /* default white on black */
  73.   win->_tabsize   = 8;
  74.   win->_clear     = FALSE;
  75.         /* (bool) ((num_lines == LINES) && (num_columns == COLS)); */
  76.   win->_leave     = FALSE;
  77.   win->_scroll    = FALSE;
  78.   win->_nodelay   = FALSE;
  79.   win->_keypad    = FALSE;
  80.   win->_regtop    = 0;
  81.   win->_regbottom = num_lines - 1;
  82.  
  83.   /* init to say window unchanged */
  84.  
  85.   for (i = 0; i < num_lines; i++)
  86.     {
  87.     win->_minchng[i] = 0;
  88.     win->_maxchng[i] = num_columns-1;
  89.     }
  90.  
  91.   /* set flags for window properties */
  92.  
  93.   if ((begy + num_lines) == LINES)
  94.     {
  95.     win->_flags |= _ENDLINE;
  96.     if ((begx == 0) && (num_columns == COLS) && (begy == 0))
  97.       win->_flags |= _FULLWIN;
  98.     } /* if */
  99.   if (((begy + num_lines) == LINES)
  100.         &&
  101.       ((begx + num_columns) == COLS))
  102.     win->_flags |= _SCROLLWIN;
  103.   return(win);
  104.   } /* makenew */
  105.  
  106. /****************************************************************/
  107. /* Newwin() creates a new window with size num_lines * num_co-    */
  108. /* lumns, and origin begx,begy relative to the SCREEN. Special    */
  109. /* case: if num_lines and/or num_columns is 0, the remainder of    */
  110. /* the screen is used.                        */
  111. /****************************************************************/
  112.  
  113. WINDOW *newwin(num_lines, num_columns, begy, begx)
  114.   int    num_lines, num_columns, begy, begx;
  115.   {
  116.   WINDOW    *win;
  117.   int        *ptr;
  118.   int         i, j;
  119.  
  120.   if (num_lines == 0)
  121.     num_lines = LINES - begy;
  122.   if (num_columns == 0)
  123.     num_columns = COLS - begx;
  124.   if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
  125.     return((WINDOW *) ERR);
  126.   for (i = 0; i < num_lines; i++)    /* make and clear the lines */
  127.     {
  128.     if((win->_line[i] = (int *) calloc(num_columns,sizeof(int))) == NULL)
  129.       {
  130.       for (j = 0; j < i; j++)        /* if error, free all the data */
  131.     free(win->_line[j]);
  132.       free(win->_minchng);
  133.       free(win->_maxchng);
  134.       free(win->_line);
  135.       free(win);
  136.       return((WINDOW *) ERR);
  137.       } /* if */
  138.     else
  139.       for (ptr = win->_line[i]; ptr < win->_line[i] + num_columns;)
  140.     *ptr++ = ' ' | ATR_NRM;
  141.     } /* for */
  142.   return(win);
  143.   } /* newwin */
  144.  
  145. /****************************************************************/
  146. /* Subwin() creates a sub-window in the 'orig' window, with    */
  147. /* size num_lines * num_columns, and with origin begx, begy    */
  148. /* relative to the SCREEN. Special case: if num_lines and/or    */
  149. /* num_columns is 0, the remainder of the original window is    */
  150. /* used. The subwindow uses the original window's line buffers    */
  151. /* to store it's own lines.                    */
  152. /****************************************************************/
  153.  
  154. WINDOW *subwin(orig, num_lines, num_columns, begy, begx)
  155.   WINDOW    *orig;
  156.   int         num_lines, num_columns, begy, begx;
  157.   {
  158.   WINDOW    *win;
  159.   int         i, j, k;
  160.  
  161.   /* make sure window fits inside the original one */
  162.  
  163.   if (
  164.       begy < orig->_begy || 
  165.       begx < orig->_begx ||
  166.       (begy + num_lines) > (orig->_begy + orig->_maxy) ||
  167.       (begx + num_columns) > (orig->_begx + orig->_maxx)
  168.      )
  169.     return((WINDOW *) ERR);
  170.  
  171.   if (num_lines == 0)
  172.     num_lines = orig->_maxy - (begy - orig->_begy);
  173.   if (num_columns == 0)
  174.     num_columns = orig->_maxx - (begx - orig->_begx);
  175.   if ((win = makenew(num_lines, num_columns, begy, begx)) == (WINDOW *) ERR)
  176.     return((WINDOW *) ERR);
  177.  
  178.   /* set line pointers the same as in the original window */
  179.  
  180.   j = begy - orig->_begy;
  181.   k = begx - orig->_begx;
  182.   for (i = 0; i < num_lines; i++)
  183.     win->_line[i] = (orig->_line[j++]) + k;
  184.   win->_flags |= _SUBWIN;
  185.   return(win);
  186.   } /* subwin */
  187.